// pages/card-msg-add/card-msg-add.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jobType: [],
    areaType: [],
    jobIndex: '',
    areaIndex: -1,
    add_mine_img:[],
    areaValue:'',
  },

  bindPickerChange: function(e) { //所属行业
    let that = this;
    let idx = e.detail.value;
    that.setData({
      jobIndex: idx
    })
  },
  bindAreaPickerChange: function (e) { //所属区域
    let that = this;
    let idx = e.detail.value;
    that.setData({
      areaValue: idx
    })
  },



  //添加图片
  chooseIMG: function(e) {
    let that = this;
    let type = e.currentTarget.dataset.type;
    let { add_mine_img } = that.data;

    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePaths可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;

        wx.uploadFile({
          url: app.globalData.host + 'common/uploadimg',
          filePath: tempFilePaths[0],
          name: 'file',
          success: function(res) {
            console.log(res)
            res = JSON.parse(res.data);
            // res = JSON.parse(res);
            add_mine_img[0]=res.filesrc;

            that.setData({
              add_mine_img,
            })
          },
          fail: function(res) {},
          complete: function(res) {},
        })
        console.log(that.data.add_mine_img)

      },
    })
  },

  removePhoto: function(e) { //移除图片
    let that = this;
    let idx = e.currentTarget.dataset.idx
    let type = e.currentTarget.dataset.type
    let { add_mine_img } = that.data;
    
    let list = add_mine_img;
    list.splice(idx, 1)
    that.setData({
      add_mine_img: list
    })


  },


  getFormId(e) { //提交
    console.log(e)
    var that = this;
    var { jobType, jobIndex, add_mine_img, areaIndex, areaType, areaValue } = that.data;
    var formId = e.detail.formId;
    var { name, phone, email,wechat, companyName, work, jobTypename, call, fax, companyAddress,high_tech, output,textarea } = e.detail.value;
    if (name == '') {
      wx.showToast({
        title: '请输入姓名！',
        icon: 'none'
      })
      return;
    } else if (phone == '' || phone.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号！',
        icon: 'none'
      })
      return;
    }else if (companyName == '' && 0) {
      wx.showToast({
        title: '请输入公司名称！',
        icon: 'none'
      })
      return;
    } else if (work == '') {
      wx.showToast({
        title: '请输入公司职务！',
        icon: 'none'
      })
      return;
    } else if (jobTypename == '') {
      wx.showToast({
        title: '请选择所属行业！',
        icon: 'none'
      })
      return;
    }else if (wechat == '') {
      wx.showToast({
        title: '请填写微信号！',
        icon: 'none'
      })
      return;
    }else if (high_tech == '') {
      wx.showToast({
        title: '请选择是否为高新企业',
        icon: 'none'
      })
      return;
    }else if (output == '') {
      wx.showToast({
        title: '请输入企业年产值',
        icon: 'none'
      })
      return;
    } else if (!areaType[areaValue]){
      wx.showToast({
        title: '请选择所属区域',
        icon: 'none'
      })
      return;
    } else { //提交
      wx.requestSubscribeMessage({
        tmplIds: ['rTy3RHLVn-uV4uNpZE2sl2ukLTpSkAWd6TcSxTwgGCs'],
        success(res) {
          let temid = 'rTy3RHLVn-uV4uNpZE2sl2ukLTpSkAWd6TcSxTwgGCs'
          var params = {
            user_id: wx.getStorageSync('userId'),
            phone: phone,
            username: name,
            email: email,
            com_name: companyName,
            com_position: work,
            com_area: jobType[jobIndex].id,
            user_area: areaType[areaValue],
            telphone: call,
            fax: fax,
            com_address: companyAddress,
            cardimg:add_mine_img[0],
            introduce: textarea,
            wechat:wechat,
            high_tech:high_tech,
            output:output
          };
          if (res[temid] == 'accept') {
            params.check = 1
            app.getFormId(formId);
            that.refer(params)
          } else {
            params.check = 0
            app.getFormId(formId);
            that.refer(params)
          }
        }
      })
     

    }
  },
  refer(params){
    wx.request({
      url: app.globalData.host + 'member/createCard',
      // name, phone, idcard, manname, depart, company
      data: params,
      method: 'POST',
      success: function(res) {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })

        if (res.data.code == 1) {
          // setTimeout(function() {
          //   wx.navigateTo({
          //     url: '/pages/my-card/my-card',
          //   })
          // }, 1500)
        }

        console.log(res)
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    var user_id = wx.getStorageSync('userId')
    var add_mine_img = that.data.add_mine_img;
    wx.request({
      url: app.globalData.host + 'member/getUserInfo',
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        user_id: user_id
      },
      success: function(res) {
        console.log(res)
        console.log(res.data.data.user)
        var jobType = that.data.jobType;
        var areaValue = that.data.areaValue;
        var jobIndex = 0;
        (jobType || []).map((v, k) => {
          if (res.data.data.user.com_area == v.id) {
            jobIndex = k;
          }
        })
        var areaType = res.data.data.userArea;
        var areaIndex = 0;
        (areaType || []).map((v, k) => {
          if (res.data.data.user.user_area == v) {
            areaValue = k;
          }
        })
        if (res.data.data.user.cardimg){
           add_mine_img[0] = res.data.data.user.cardimg;
        } else {
          add_mine_img=[]
        }
        that.setData({
          user: res.data.data.user,
          add_mine_img,
          jobIndex: jobIndex,
          areaValue: areaValue, 
          areaType: areaType, 
        })
        wx.hideLoading();
      }
    })

    that.getJobType();
  },


  getJobType: function () { //搜索行业分类
    var that = this;
    wx.request({
      url: app.globalData.host + 'company/getCompanyArea',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        wx.setStorageSync('AssocType', res.data.data)
        that.setData({
          jobType: res.data.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },


})